﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;
using ZhimeiSoft.Framework.Client.Control.DateTimeGrid;
using ZhimeiSoft.Framework.Client.Control;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class CustomReservationForMonth : BaseUserControl
    {
        private DataSet dsReservationDetail = null;
        public DBEntityCustomInfo customInfo = null;
        private DBEntityReservation reservationInfo = null;

        public DBEntityReservation getCurrReservation
        {
            get
            {
                return reservationInfo;
            }
        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        public void refreshData()
        {
            if (customInfo == null)
            {
                return;
            }
            DateTime queryTime = monthDateTimeForMonth.CurrDateTimeViewDayControl.GetDateTime();
            InitData(customInfo, queryTime);
        }

        public CustomReservationForMonth()
        {
            InitializeComponent();
            monthDateTimeForMonth.MonthDateTime = DateTime.Now;
            customSearch1.onSerchCustom += OnEventSerchCustom;
            monthDateTimeForMonth.OnEventCurrMonth += this.OnMonthChange;
            monthDateTimeForMonth.OnEventClickCurrDay += this.EventClickCurrDay;
        }

        public void InitData(DBEntityCustomInfo queryCustomInfo, DateTime queryTime)
        {
            string error = string.Empty;
            labCustomName.Text = "会员：" + queryCustomInfo.CNAME;
            labNickName.Text = "昵称：" + queryCustomInfo.CNICKNAME;
            labTelPhone.Text = "电话：" + queryCustomInfo.CMOBILEPHONE;
            labLastRegister.Text = "上次到店：" + queryCustomInfo.CLASTREGISTERDATE.ToShortDateString() + " " + queryCustomInfo.CLASTREGISTERDATE.ToShortTimeString();

            string sfilter = "CUSTOMINFO." + DBEntityCustomInfo.FieldName.CUSTOMERID + " = " + queryCustomInfo.CUSTOMERID;
            if (BussinessProxyManager.Instance != null)
            {
                if (!BussinessProxyManager.Instance.QueryReservationMonth(queryTime.Year, queryTime.Month, sfilter, out dsReservationDetail, out error))
                {
                    MsgBox.MsgInfo("获取预约信息错误：" + error);
                    return;
                }
                monthDateTimeForMonth.DataBindForMonth(dsReservationDetail, DBEntityCustomInfo.FieldName.CNICKNAME,
                    DBEntityReservation.FieldName.RESERVATIONID, DBEntityReservation.FieldName.RESERVATIONTIME);
            }
            grdReservationDetail.DataSource = null;

        }
        private void OnEventSerchCustom(DataRow drCustomInfo)
        {
            if(drCustomInfo == null)
                return;

            customInfo = new DBEntityCustomInfo(drCustomInfo);
            DateTime queryTime = monthDateTimeForMonth.CurrDateTimeViewDayControl.GetDateTime();
            InitData(customInfo, queryTime);
        }

        private void grvReservationDetail_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
            {
                return;
            }
            if (e.Column.FieldName.Equals(DBEntityReservationServiceItem.FieldName.RESERVATIONSTATE))
            {
                if (e.CellValue == null || e.CellValue.ToString() == string.Empty)
                    return;

                string displayText = DBEntityReservationServiceItem.ENUM_RESERVATIONSTATE.EnumStrToChineseStr(e.CellValue.ToString());
                if (displayText.Equals(string.Empty))
                    return;
                e.DisplayText = displayText;
            }
        }

        private void OnMonthChange(DateTime currMonth)
        {
            if (customInfo != null)
            {
                InitData(customInfo, currMonth);
            }
        }

        private void EventClickCurrDay(object iDateTimeView)
        {
            MonthDateTimeForDay currDateTimeViewControl = (MonthDateTimeForDay)iDateTimeView;
            if (currDateTimeViewControl.GetCurrValue() == null)
            {
                return;
            }
            int reservationID = Convert.ToInt16(currDateTimeViewControl.GetCurrValue());
            string error = string.Empty;
            DataSet dsReservationDetail = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationServiceItemDetail(reservationID, out dsReservationDetail, out error))
            {
                MsgBox.MsgInfo("获取预约明细数据出现错误：" + error);
                return;
            }
            DataSet dsReservation = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(reservationID, out dsReservation, out error))
            {
                MsgBox.MsgInfo("获取预约信息错误：" + error);
            }
            if (dsReservation.Tables[1].Rows.Count != 0)
            {
                reservationInfo = new DBEntityReservation(dsReservation.Tables[1].Rows[0]);
            }
            else
            {
                reservationInfo = null;
            }
            grdReservationDetail.DataSource = dsReservationDetail.Tables[0];
        }

        private void grvReservationDetail_Click(object sender, EventArgs e)
        {

        }
    }
}
